package hlhj.fhp.newslib.adapters

import android.util.Log
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.tenma.ventures.bean.utils.TMSharedPUtil
import com.tenma.ventures.config.TMServerConfig
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.customview.GoLoginDialog
import hlhj.fhp.newslib.javabean.CommentBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.utils.EmptyUtil
import hlhj.fhp.newslib.utils.GlideUtil
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\27 0027.
 */
class CommentAdp(private val layoutId:Int,private val datas
:ArrayList<CommentBean.DataBean>) :BaseQuickAdapter<CommentBean.DataBean,BaseViewHolder>(layoutId,datas) {
    override fun convert(helper: BaseViewHolder?, item: CommentBean.DataBean?) {
        helper?.setText(R.id.btZan,item?.laud_num.toString())
        val btZan = helper?.getView<TextView>(R.id.btZan)
        if (item?.is_laud==1){
            val drawable = mContext.resources.getDrawable(R.drawable.ic_praise_sected)
            val view = helper?.getView<TextView>(R.id.btZan)
            view!!.setCompoundDrawablesWithIntrinsicBounds(drawable,
                    null, null, null);
        }else{
            val drawable = mContext.resources.getDrawable(R.drawable.ic_praise_normal)
            val view = helper?.getView<TextView>(R.id.btZan)
            view!!.setCompoundDrawablesWithIntrinsicBounds(drawable,
                    null, null, null);
        }
        btZan?.setOnClickListener {
            if (item?.is_laud==1){//已点赞，取消点赞
                Apis.itsGood(item?.id,TMSharedPUtil.getTMToken(mContext))
                        ?.doOnSubscribe {
                        }
                        ?.subscribe {
                            data->
                            if (data.code==1){
                                item?.is_laud=0
                                item?.laud_num=item?.laud_num-1
                                notifyDataSetChanged()
                            }else{
                                GoLoginDialog(mContext).show()
                            }
                        }
            }else{//未点赞，现在点赞
                Apis.itsGood(item?.id!!,TMSharedPUtil.getTMToken(mContext))
                        ?.doOnSubscribe {
                        }
                        ?.subscribe {
                            data->
                            if (data.code==1){
                                item?.is_laud=1
                                item?.laud_num=item?.laud_num+1
                                notifyDataSetChanged()
                            }else{
                                GoLoginDialog(mContext).show()
                            }
                        }
            }
        }

        if (!EmptyUtil.isStringNull(item?.avatar)){
            if (item?.avatar?.contains("http")!!){
                Glide.with(mContext).load(item?.avatar).apply(GlideUtil.loadIMGOption())
                        .into(helper?.getView(R.id.itemIcon)!!)
            }else{
                Glide.with(mContext).load(TMServerConfig.BASE_URL+item?.avatar).apply(GlideUtil.loadIMGOption())
                        .into(helper?.getView(R.id.itemIcon)!!)
            }
        }
        if (!EmptyUtil.isStringNull(item?.uname)){
            helper?.setText(R.id.itemName,item?.uname)
        }else{
            helper?.setText(R.id.itemName,TMSharedPUtil.getTMUser(mContext).member_name)
        }
        helper?.setText(R.id.itemTime,item?.add_time)
        helper?.setText(R.id.itemContnet,item?.content)
    }
}